﻿using System.Linq;
using System.Web.Mvc;
using Business;
using Business.DbConnect;

namespace ServerMotorManagement.Controllers
{
    [Authorize]
    public class UserController : Controller
    {
        private QLNXeEntities db = new QLNXeEntities();

        //
        // GET: /User/

        public ViewResult Index()
        {
            return View(UserBusiness.SelectList());
        }

        //
        // GET: /User/Details/5

        public PartialViewResult Details(short id)
        {
            user user = UserBusiness.Select(id);
            return PartialView("DetailsForm", user);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return PartialView("CreateForm");
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(user user)
        {
            if (ModelState.IsValid)
            {
                UserBusiness.Add(user);
                return RedirectToAction("Index");
            }

            return PartialView("CreateForm", user);
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(short id)
        {
            user user = UserBusiness.Select(id);
            return PartialView("EditForm", user);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(user user)
        {
            if (ModelState.IsValid)
            {
                UserBusiness.Update(user);
                return RedirectToAction("Index");
            }
            return PartialView("EditForm", user);
        }

        //
        // GET: /User/Delete/5

        //public ActionResult Delete(short id)
        //{
        //    user user = db.users.Find(id);
        //    return View(user);
        //}

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(short id)
        {
            UserBusiness.Delete(id);
            return PartialView("ListUser", db.users.ToList());
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}